/**
收集所有的函数定义并生成报告
**/


const babel = require('@babel/core');
const parser = require('@babel/parser');
const traverse = require('@babel/traverse').default;

// 要转换的代码
const code = `
function foo() {}
const bar = function() {};
const baz = () => {};
function qux(a, b) {
    return a + b;
}
`;

// 解析代码为 AST
const ast = parser.parse(code);

// 定义 visitor 对象
const functions = [];
const visitor = {
    FunctionDeclaration(path) {
        functions.push({
            name: path.node.id.name,
            params: path.node.params.length,
            type: 'declaration'
        });
    },
    FunctionExpression(path) {
        functions.push({
            name: path.parent.id ? path.parent.id.name : 'anonymous',
            params: path.node.params.length,
            type: 'expression'
        });
    },
    ArrowFunctionExpression(path) {
        functions.push({
            name: 'anonymous',
            params: path.node.params.length,
            type: 'arrow'
        });
    }
};

// 遍历 AST
traverse(ast, visitor);

// 生成报告
console.log('Function Report:');
functions.forEach(func => {
    console.log(`Name: ${func.name}, Params: ${func.params}, Type: ${func.type}`);
});
// 输出
// Function Report:
// Name: foo, Params: 0, Type: declaration
// Name: bar, Params: 0, Type: expression
// Name: anonymous, Params: 0, Type: arrow
// Name: qux, Params: 2, Type: declaration